package com.wiscamp.ninechapters.problems.infrastructure.repositories;

import com.wiscamp.ninechapters.problems.domain.models.challenges.AmcStatistic;
import com.wiscamp.ninechapters.problems.domain.models.reports.TaskReportContent;
import com.wiscamp.ninechapters.problems.domain.repositories.TaskReportRepository;
import com.wiscamp.ninechapters.problems.infrastructure.mappers.challenges.AmcStatisticMapper;
import com.wiscamp.ninechapters.problems.infrastructure.mappers.reports.TaskReportContentMapper;
import com.wiscamp.ninechapters.problems.infrastructure.po.challenges.AmcStatisticPO;
import com.wiscamp.ninechapters.problems.infrastructure.po.reports.TaskReportContentPO;
import cube.ddd.aspect.HandleDataException;
import cube.ddd.domain.BaseRepository;
import jakarta.validation.constraints.Min;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;

import java.util.Objects;

@Repository
public class TaskReportRepositoryImpl extends BaseRepository implements TaskReportRepository {
    private final TaskReportContentMapper taskReportContentMapper;

    private final AmcStatisticMapper amcStatisticMapper;

    public TaskReportRepositoryImpl(TaskReportContentMapper taskReportContentMapper, AmcStatisticMapper amcStatisticMapper) {
        this.taskReportContentMapper = taskReportContentMapper;
        this.amcStatisticMapper = amcStatisticMapper;
    }


    //region converter
    public TaskReportContent toTaskReportContent(TaskReportContentPO po) {
        var reportContent = new TaskReportContent();
        BeanUtils.copyProperties(po, reportContent);
        return reportContent;
    }

    public TaskReportContentPO toTaskReportContentPO(TaskReportContent reportContent) {
        var po = new TaskReportContentPO();
        po.setReportContent(reportContent.getReportContent());
        po.setTaskId(reportContent.getTaskId());
        po.setId(reportContent.getId());
        //BeanUtils.copyProperties(reportContent, po);
        return po;
    }


    public AmcStatistic toAmcStatistic(AmcStatisticPO po) {
        var entity = new AmcStatistic();
        BeanUtils.copyProperties(po, entity);
        entity.setPercentThreeQuarterPoints(po.getPercentThreequanterPoints());
        return entity;
    }

    //endregion


    // region task report

    @HandleDataException
    public TaskReportContent getReportContent(@Min(0) long taskId) {
        var po = taskReportContentMapper.findByTask(taskId);
        if (Objects.nonNull(po)) {
            return toTaskReportContent(po);
        }
        return null;
    }

    @HandleDataException
    public TaskReportContent saveReportContent(TaskReportContent reportContent) {
        var po = toTaskReportContentPO(reportContent);
        if (Objects.nonNull(po)) {
            var savedPO = taskReportContentMapper.saveAndFlush(po);
            return toTaskReportContent(savedPO);
        }
        return null;
    }

    // endregion


    // region amc
    public AmcStatistic getAmcStatistic(long examId, int year, String abPaper) {
        var statisticPO = amcStatisticMapper.findAmcStatistic(examId, year, abPaper);
        if (Objects.nonNull(statisticPO))
            return toAmcStatistic(statisticPO);
        return null;
    }
    //endregion
}
